package com.cy.pj.goods.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.*;

@Repository//特殊的@Component
public class DefaultGoodsDao implements GoodsDao{

    @Autowired
    private DataSource dataSource;

    private static final Logger log = LoggerFactory.getLogger(DefaultGoodsDao.class);



    @Override
    public List<Map<String,Object>> findGoods(){
        List<Map<String,Object>> list=new ArrayList<>();
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        Map<String,Object> map;
        try {
            //1.获取连接
           connection = dataSource.getConnection();
            //2.创建statement
            statement = connection.createStatement();
            //3.发送sql
            String sql = "select * from tb_goods";

            //4.处理结果
            resultSet = statement.executeQuery(sql);



            ResultSetMetaData rsmd = resultSet.getMetaData();

            while(resultSet.next()){
                map = new HashMap<>();
               map.put("id", resultSet.getInt("id"));
               map.put("name", resultSet.getString("name"));
               map.put("remark", resultSet.getString("remark"));
               map.put("createdTime", resultSet.getDate("createdTime"));
               list.add(map);
            }
        } catch (SQLException throwables) {
          return null;
        }finally {
            //5.释放资源
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

}
